package com.cv4j.app.fragment;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.core.datamodel.ByteProcessor;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.core.hist.CalcHistogram;
import com.cv4j.core.hist.CompareHist;
import com.cv4j.core.hist.EqualHist;

/**
 * 直方图比较-原图和直方图均衡化界面
 *
 * @since 2021-05-18
 */
public class CompareHist1Fragment {
    private Context mContext;
    private Component component;
    private Image image0;
    private Image image1;
    private Text result;

    private CompareHist1Fragment(Context context) {
        mContext = context;
        init();
    }

    /**
     * CompareHist1Fragment
     *
     * @param context context
     * @return CompareHist1Fragment
     */
    public static CompareHist1Fragment getInstance(Context context) {
        CompareHist1Fragment compareHist1Fragment = new CompareHist1Fragment(context);
        return compareHist1Fragment;
    }

    private void init() {
        component = LayoutScatter.getInstance(mContext)
            .parse(ResourceTable.Layout_fragment_compare_hist_1, null, false);
        component.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
            ComponentContainer.LayoutConfig.MATCH_PARENT));
        image0 = (Image) component.findComponentById(ResourceTable.Id_image0);
        image1 = (Image) component.findComponentById(ResourceTable.Id_image1);
        result = (Text) component.findComponentById(ResourceTable.Id_result);
        initData();
    }

    private void initData() {
        PixelMap pixelMap = PixelMapUtil.getPixelMapFromResource(mContext, ResourceTable.Media_test_hist).get();
        image0.setPixelMap(pixelMap);
        CV4JImage cv4jImage = new CV4JImage(pixelMap);
        ImageProcessor imageProcessor = cv4jImage.convert2Gray().getProcessor();
        int[][] source;
        int[][] target = new int[0][];
        CalcHistogram calcHistogram = new CalcHistogram();
        int bins = 180;
        source = new int[imageProcessor.getChannels()][bins];
        calcHistogram.calcHSVHist(imageProcessor, bins, source, true);
        if (imageProcessor instanceof ByteProcessor) {
            EqualHist equalHist = new EqualHist();
            equalHist.equalize((ByteProcessor) imageProcessor);
            image1.setPixelMap(cv4jImage.getProcessor().getImage().toBitmap());
            target = new int[imageProcessor.getChannels()][bins];
            calcHistogram.calcHSVHist(imageProcessor, bins, target, true);
        }
        CompareHist compareHist = new CompareHist();
        StringBuilder sb = new StringBuilder();
        sb.append("巴氏距离:").append(compareHist.bhattacharyya(source[0], target[0])).append(System.lineSeparator())
            .append("协方差:").append(compareHist.covariance(source[0], target[0])).append(System.lineSeparator())
            .append("相关性因子:").append(compareHist.ncc(source[0], target[0]));
        result.setText(sb.toString());
    }

    public Component getComponent() {
        return component;
    }
}
